package com.hunttown.common.sharding.client.rule.function;

public class Mod extends Function<Long> {
    private int factor;

    public Mod(int factor) {
        if (factor <= 0) {
            throw new IllegalArgumentException("factor must be positive(>0)!");
        } else {
            this.factor = factor;
        }
    }

    public long eval(Long longValue) {
        if (longValue == null) {
            throw new IllegalArgumentException("[Function#Mod]longValue cann't be null!");
        } else {
            return longValue.longValue() % (long)this.factor;
        }
    }

    Long parseInput(Object obj) {
        if (obj instanceof Integer) {
            return Long.parseLong(String.valueOf(obj));
        } else if (obj instanceof Long) {
            return (Long)obj;
        } else if (obj instanceof String) {
            return Long.parseLong((String)obj);
        } else {
            throw new IllegalArgumentException("[Function#Mod]Invalid input Object!");
        }
    }
}